﻿using GameLogic;
using NPOI.SS.Formula.Functions;
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.UI;


[CustomEditor(typeof(ShipGenerator))]
[CanEditMultipleObjects]
public class ShipGeneratorEditor : Editor
{
    
    private SerializedProperty _ConfigName;
    private SerializedProperty _ShipID;
    List<string> m_ConfigList = new List<string>();
    string m_OldShipID = "";
    protected  void OnEnable()
    {
        _ConfigName = serializedObject.FindProperty("m_ConfigName");
        _ShipID = serializedObject.FindProperty("m_ShipID");
        RebuildConfigList();
    }
    
    public void RebuildConfigList()
    {
        m_OldShipID = _ShipID.intValue.ToString();
        var fileDir = ShipModule.ShipConfigDir + m_OldShipID + "/";

        DirectoryInfo directoryInfo = new DirectoryInfo(fileDir);
        if (!directoryInfo.Exists)
            return;
        var files = directoryInfo.GetFiles();
        m_ConfigList.Clear();
        foreach (var i in files)
        {
            var fileNameArray = i.Name.Split('.');
            var name = fileNameArray[0];
            var suffix = fileNameArray[1];
            if(suffix == "cfg")
                m_ConfigList.Add(name);
        }
    }
    public override void OnInspectorGUI() {

        base.OnInspectorGUI();
        serializedObject.Update();

        if(_ShipID.intValue.ToString() != m_OldShipID)
            RebuildConfigList();

        if(m_ConfigList.Count > 0)
        {
            var selectedIndex = 0;
            for (var i = 0; i < m_ConfigList.Count; ++i)
            {
                if (m_ConfigList[i] == _ConfigName.stringValue)
                {
                    selectedIndex = i;
                    break;
                }
            }
            var idx = EditorGUILayout.Popup(selectedIndex, m_ConfigList.ToArray());
            _ConfigName.stringValue = m_ConfigList[idx];
        }
        else
        {
            EditorGUILayout.LabelField("没有配置文件");
        }
        serializedObject.ApplyModifiedProperties();
    }
}